// pages/posts/post-details/post-details.js
var postsData = require('../../../data/postts-data.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentPostId:0,
        collected: false,
        isPlayingMusic: false
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var postId = options.id;
        this.data.currentPostId = postId;
        var postData = postsData.postList[postId]
        // 如果在onload方法中，不是异步的去执行一个数据绑定，则不需要使用this.setData方法
        // 只需要对this.data赋值即可实现数据绑定
        var postData = postsData.postList[postId];
        this.setData({
            postData: postData
        })
        /**
         * var postsCollected ={
        *   1:"true",
        *   2:"false",
        *   3:"true",
         * ....
         * }
         */
        // 异步本地存储,需要主动清楚，否则一直存在
        // 在本地存储一个key
        var postsCollected = wx.getStorageSync('posts_collected')
        // 判断是否存在
        if (postsCollected) {
            var postCollected = postsCollected[postId];
            if (postCollected){
                this.setData({
                    collected: postCollected
                })
            }
        }else{
            var postsCollected = {}
            postsCollected[postId] = false;
            wx.setStorageSync('posts_collected', postsCollected);
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        console.log('分享')
    },
    // 用户收藏功能
    onCollectionTap:function(event){
        this.getPostsCollectedAsy()
        // this.getPostsCollectedSyc()
    },
    // 异步方法，异步里面异步带有success回调
    getPostsCollectedAsy: function () {
        var that = this;
        wx.getStorage({
            key: 'posts_collected',
            success(res) {
                var postsCollected = res.data;
                var postCollected = postsCollected[that.data.currentPostId];
                // 收藏变成未收藏
                postCollected = !postCollected;
                postsCollected[that.data.currentPostId] = postCollected;
                that.setData({
                    collected: postCollected
                })
                that.showToast(postsCollected, postCollected);
            }
        })
    },
    // 同步方法,一般最好使用同步，同步比较安全
    getPostsCollectedSyc: function () {
        var postsCollected = wx.getStorageSync('posts_collected');
        var postCollected = postsCollected[this.data.currentPostId];
        // 收藏变成未收藏，未收藏变成收藏
        postCollected = !postCollected;
        postsCollected[this.data.currentPostId] = postCollected;
        this.showToast(postsCollected, postCollected);
    },
    // 收藏提示框
    showToast: function (postsCollected, postCollected) {
        // 更新文章是否有缓存值
        wx.setStorageSync('posts_collected', postsCollected);
        // 更新数据绑定变量，从而实现切换图片
        this.setData({
            collected: postCollected
        })
        wx.showToast({
            title: postCollected ? "收藏成功":"取消成功",
            duration:1000,
            icon:"success"
        })
        // wx.showModal({
        //     title: '收藏',
        //     content: postCollected ? "收藏该文章？" : "取消收藏该文章？",
        //     cancelText:"取消",
        //     cancelColor:"#333",
        //     confirmText:"确认",
        //     confirmColor:"blue"
        // })

    },
    // 用户分享
    onShowTap:function(event) {
        var itemList = [
            "分享给微信好友",
            "分享到朋友圈",
            "分享到QQ",
            "分享到微博"
        ];
        // 显示操作菜单
        wx.showActionSheet({
            itemList: itemList,
            itemColor:"#405f80",
            success:function(res) {
                // res.cansel 用户是不是点击了取消按钮
                // res.tapIndex 数组元素的序号，从0开始
                wx.showModal({
                    title: '用户' + itemList[res.tapIndex],
                    content: '1111',
                })
            }
        })
    },
    // 点击播放音乐
    onMusicTap:function(event) {
        var currentPostId = this.data.currentPostId;
        var postData = postsData.postList[currentPostId];
        this.data.isPlayingMusic = true
        console.log(this.data.isPlayingMusic)
        const backgroundAudioManager = wx.getBackgroundAudioManager()
        backgroundAudioManager.title = '此时此刻'
        backgroundAudioManager.epname = '此时此刻'
        backgroundAudioManager.singer = '许巍'
        backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
        // 设置了 src 之后会自动播放
        backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    }
})